

Partial Class pages_Products_Product : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property ProductId() As Integer
        Get
            Return CWeb.RequestInt("productId")
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Product]() As CProduct
        Get
            Product = CProduct.Cache.GetById(ProductId)
            If IsNothing(Product) Then CSitemap.RecordNotFound("Product", ProductId)
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.ProductEdit(Me.Product.ProductId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        With Product
            UnbindSideMenu()
            AddMenuSide(.ProductType.Plural & "...", CSitemap.Products(Nothing, Product.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
            AddMenuSide(Product.TypeName & " Details", CSitemap.ProductEdit(ProductId), True)
            AddMenuSide("Image" & IIf(.HasImage, "*", String.Empty), CSitemap.ProductImage(ProductId))
            If .IsSet Then AddMenuSide(CUtilities.NameAndCount("Contents", .Children), CSitemap.ProductContents(ProductId))
            If .CanHaveTranscript Then
                AddMenuSide("Transcription" & IIf(.HasTranscript, "*", String.Empty), CSitemap.ProductTranscription(ProductId))
            End If
            AddMenuSide("Vimeo Link" & IIf(.HasVideosOnVimeo, "*", String.Empty) & IIf(.HasVideosOnYouTube, "*", String.Empty), CSitemap.ProductVimeo(ProductId))
            If .CanHaveFiles Then
                AddMenuSide(CUtilities.NameAndCount("Dropbox Files", .Files), CSitemap.ProductFiles(ProductId))
            Else
                AddMenuSide(CUtilities.NameAndCount("Epub/Mobi eBooks", CFile.Cache.GetByProductId(Me.ProductId)), CSitemap.ProductFiles(ProductId))
            End If
            If Not .IsFree OrElse .PaymentsCount > 0 OrElse .OrdersCountCached > 0 Then
                AddMenuSide(CUtilities.NameAndCount("Orders", .OrdersCountCached), CSitemap.OrdersForProduct(ProductId))
                AddMenuSide(CUtilities.NameAndCount("Payments", .PaymentsCount), CSitemap.PaymentsForProduct(ProductId))
            End If
            If .HasParent Then AddMenuSide("Back to Set...", CSitemap.ProductContents(.ProductBelongsToProductId))


            Me.Title = .TypeName & " Details"

            txtCreated.ShowAmPm = .ShowDate

            If .CanHaveParent Then
                ddProductBelongsToProductId.DataSource = CProduct.Cache.ConferenceSets
                ddProductBelongsToProductId.DataBind()
                ddProductBelongsToProductId.BlankItem("-- Standalone --")
                ddProductBelongsToProductId.ValueInt = .ProductBelongsToProductId
                ddProductBelongsToProductId.Required = .ParentIsRequired
            Else
                ddProductBelongsToProductId.Visible = False
            End If
        End With


        With ddProductTypeId
            .DataTextField = "TypeName"
            .DataValueField = "TypeId"
            .DataSource = CProductType.Cache
            .DataBind()
            ' .BlankItem("-- Select Type --")
        End With

        If Not CSession.IsDeveloper Then
            ddProductTypeId.Mode = EControlMode.Locked
            ddProductTypeId.Required = False
        End If


        ddProductSpeakerId.DataSource = CSpeaker.Cache
        ddProductSpeakerId.DataBind()

        ddProductSpeaker2Id.DataSource = CSpeaker.Cache
        ddProductSpeaker2Id.DataBind()
        ddProductSpeaker2Id.BlankItem("-- Second Speaker --", 0)

        ddProductLanguageId.DataSource = CLanguage.Cache
        ddProductLanguageId.DataBind()

        ddProductLocationId.DataSource = CLocation.Cache
        ddProductLocationId.DataBind()
        ddProductLocationId.BlankItem("-- N/A --", 0)

        With ddProductPaypalButtonId
            .DataSource = CButton.Cache
            .DataBind()
            .BlankItem("-- Select Button --")
        End With


        ddProductPaypalButtonId.Visible = Not Product.IsFree


        ddProductPaypalButtonId.NavigateUrl = CSitemap.ButtonAdd()
        ddProductPaypalButtonId.HyperlinkText = "Add a Button..."




        ddProductTypeId.AutoPostback = False

        With Product
            If .Files.Count > 0 OrElse .Children.Count > 0 Then
                ddProductTypeId.Visible = CSession.IsDeveloper 'Mode = EControlMode.Locked
                ddProductTypeId.ToolTip = "Cannot change the type once Files or Child-Products have been added"
                ddProductTypeId.ValueInt = Product.ProductTypeId
            End If

            If .HasParent Then
                ddProductBelongsToProductId.NavigateUrl = CSitemap.ProductEdit(.ProductBelongsToProductId)
                ddProductBelongsToProductId.HyperlinkText = String.Concat("View ", .BelongsTo.TypeName, "...")
            End If


            ddProductSpeakerId.NavigateUrl = CSitemap.SpeakerEdit(.ProductSpeakerId)
            ddProductSpeakerId.HyperlinkText = String.Concat("Edit ", .SpeakerName, "...")

            If .ProductSpeaker2Id > 0 Then
                ddProductSpeaker2Id.NavigateUrl = CSitemap.SpeakerEdit(.ProductSpeaker2Id)
                ddProductSpeaker2Id.HyperlinkText = String.Concat("Edit ", .Speaker2Name, "...")
            End If

            If .ProductTypeId_ = EProductType.Paperback Then
                txtCreatespace.Required = True
                txtProductKeywords.Label = "Text below Image"
                txtSmashwords.Visible = False
                ddProductBelongsToProductId.Visible = False
            End If

            If .ProductLanguageId <> ELanguage.English Then
                txtProductNameChineseSimplified.Visible = True
                txtProductDescriptionChineseSimplified.Visible = True
                txtProductKeywordsChineseSimplified.Visible = True

                If .ProductLanguageId = ELanguage.Chinese Then
                    txtProductNameChineseTraditional.Visible = True
                    txtProductDescriptionChineseTraditional.Visible = True
                    txtProductKeywordsChineseTraditional.Visible = True
                End If
            End If
        End With


        'Textbox logic
        Me.Form.DefaultButton = btnSave.UniqueID

        'Button Text
        AddButton(CSitemap.ProductAdd(Product.ProductTypeId), "Create a new Product")
    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostBack Then Exit Sub

        LoadProduct()
    End Sub
    Protected Overrides Sub OnPreRender(e As System.EventArgs)
        With Product
            ddProductTypeId.ValueInt = .ProductTypeId
        End With
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'If Not Me.IsValid() Then Exit Sub
        SaveProduct()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim p As CProduct = Me.Product
        p.Delete()

        If IO.File.Exists(Server.MapPath(CSitemap.ProductImage(p))) Then IO.File.Delete((Server.MapPath(CSitemap.ProductImage(p))))
        If IO.File.Exists(Server.MapPath(CSitemap.ProductThumb(p))) Then IO.File.Delete((Server.MapPath(CSitemap.ProductThumb(p))))
        If IO.File.Exists(Server.MapPath(CSitemap.ProductTiny(p))) Then IO.File.Delete((Server.MapPath(CSitemap.ProductTiny(p))))

        If p.HasParent Then
            Response.Redirect(CSitemap.ProductContents(p.ProductBelongsToProductId))
        Else
            Response.Redirect(CSitemap.Products(Nothing, p.ProductTypeId, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Integer.MinValue, Nothing))
        End If
    End Sub


#End Region

#Region "Private - Load/Save"
    Protected Sub LoadProduct()
        With Me.Product
            txtProductName.Text = .ProductName
            ddProductTypeId.ValueInt = .ProductTypeId
            ddProductLanguageId.ValueInt = .ProductLanguageId
            ddProductLocationId.ValueInt = .ProductLocationId
            ddProductSpeakerId.ValueInt = .ProductSpeakerId
            ddProductSpeaker2Id.ValueInt = .ProductSpeaker2Id
            ddProductBelongsToProductId.ValueInt = .ProductBelongsToProductId
            txtProductDescription.Text = .ProductDescription
            ddProductPaypalButtonId.ValueInt = .ProductPaypalButtonId
            chkProductIsActive.Checked = .ProductIsActive
            txtProductKeywords.Text = .ProductKeywords

            txtAmazon.Text = .ProductAmazonId
            If .ProductSmashwordsId > 0 Then txtSmashwords.Text = .ProductSmashwordsId
            If .ProductCSBookId > 0 Then txtCreatespace.Text = .ProductCSBookId
            If .ProductCSBook2Id > 0 Then txtCreatespace2.Text = .ProductCSBook2Id
            If .ProductCSBook3Id > 0 Then txtCreatespace3.Text = .ProductCSBook3Id

            If txtAmazon.Text.Length > 0 Then
                txtAmazon.NavigateUrl = CSitemap.External_Amazon(.ProductAmazonId, .ProductName)
                txtAmazon.Hyperlink.Text = "View in Store"
                txtAmazon.Hyperlink.Target = "_blank"
            End If
            If txtSmashwords.Text.Length > 0 Then
                txtSmashwords.NavigateUrl = String.Concat("https://www.smashwords.com/books/view/", .ProductSmashwordsId)
                txtSmashwords.Hyperlink.Text = "View in Store"
                txtSmashwords.Hyperlink.Target = "_blank"
            End If
            If txtCreatespace.Text.Length > 0 Then
                txtCreatespace.NavigateUrl = CSitemap.External_CreateSpace(.ProductCSBookId)
                txtCreatespace.Hyperlink.Text = "View in Store"
                txtCreatespace.Hyperlink.Target = "_blank"
            End If
            txtCreated.ValueDate = Product.ProductCreated

            txtProductNameChineseSimplified.Text = .ProductNameChineseSimplified
            txtProductDescriptionChineseSimplified.Text = .ProductDescriptionChineseSimplified
            txtProductKeywordsChineseSimplified.Text = .ProductKeywordsChineseSimplified

            txtProductNameChineseTraditional.Text = .ProductNameChineseTraditional
            txtProductDescriptionChineseTraditional.Text = .ProductDescriptionChineseTraditional
            txtProductKeywordsChineseTraditional.Text = .ProductKeywordsChineseTraditional
        End With
    End Sub
    Protected Sub SaveProduct()
        With Me.Product
            If ddProductTypeId.Mode = EControlMode.Editable Then
                .ProductTypeId = ddProductTypeId.ValueInt
            End If
            .ProductLanguageId = ddProductLanguageId.ValueInt
            .ProductLocationId = ddProductLocationId.ValueInt
            .ProductBelongsToProductId = ddProductBelongsToProductId.ValueInt
            .ProductName = txtProductName.Text
            .ProductDescription = txtProductDescription.Text
            .ProductKeywords = txtProductKeywords.Text
            .ProductPaypalButtonId = ddProductPaypalButtonId.ValueInt
            .ProductIsActive = chkProductIsActive.Checked
            .ProductSpeakerId = ddProductSpeakerId.ValueInt
            .ProductSpeaker2Id = ddProductSpeaker2Id.ValueInt

            Dim s As String = txtAmazon.Text
            If s.Length > 10 AndAlso -1 <> s.IndexOf("dp/") Then
                Dim startAt As Integer = s.IndexOf("dp/") + 3
                Dim stopAt As Integer = s.IndexOf("/", startAt)
                s = s.Substring(startAt, stopAt - startAt)
            End If
            .ProductAmazonId = s

            .ProductSmashwordsId = txtSmashwords.ValueInt
            s = txtSmashwords.Text
            If Integer.MinValue = .ProductSmashwordsId AndAlso s.Length > 0 AndAlso s.Contains("/") Then
                s = s.Substring(s.LastIndexOf("/") + 1)
                Integer.TryParse(s, .ProductSmashwordsId)
            End If

            .ProductCSBookId = txtCreatespace.ValueInt
            .ProductCSBook2Id = txtCreatespace2.ValueInt
            .ProductCSBook3Id = txtCreatespace3.ValueInt
            s = txtCreatespace.Text
            If Integer.MinValue = .ProductCSBookId AndAlso s.Length > 0 AndAlso s.Contains("/") Then
                s = s.Substring(s.LastIndexOf("/") + 1)
                Integer.TryParse(s, .ProductCSBookId)
            End If

            .ProductCreated = txtCreated.ValueDate

            If .ProductLanguageId <> ELanguage.English Then
                .ProductNameChineseSimplified = txtProductNameChineseSimplified.Text
                .ProductDescriptionChineseSimplified = txtProductDescriptionChineseSimplified.Text
                .ProductKeywordsChineseSimplified = txtProductKeywordsChineseSimplified.Text

                If .ProductLanguageId = ELanguage.Chinese Then
                    .ProductNameChineseTraditional = txtProductNameChineseTraditional.Text
                    .ProductDescriptionChineseTraditional = txtProductDescriptionChineseTraditional.Text
                    .ProductKeywordsChineseTraditional = txtProductKeywordsChineseTraditional.Text
                End If
            End If

            .Save()
        End With
        'CCache.ClearCache()
    End Sub
#End Region


End Class
